package com.study.qqclient.view;

import com.study.qqclient.service.UserClientService;
import com.study.qqclient.service.MessageClientService;
import com.study.qqclient.service.FileClientService;
import com.study.utils.Utility;

//菜单界面
public class QQView {
    private boolean loop = true;
    private String key = ""; //接收用户输入
    private final UserClientService userClientService = new UserClientService();

    public static void main(String[] args) {
        new QQView().mainMenu();
    }

    //显示登录界面
    private void mainMenu() {
        while (loop) {
            System.out.println("**********欢迎进入QQ系统**********");
            System.out.println("**********1. 登录**********");
            System.out.println("**********9. 退出**********");
            System.out.print("请选择菜单：");

            key = Utility.readString(1, "1");
            switch (key) {
                case "1":
                    System.out.println("请输入用户id：");
                    String userId = Utility.readString(50);
                    System.out.println("请输入用户密码：");
                    String password = Utility.readString(50);
                    System.out.println("userId: " + userId + " password: " + password);
                    if(userClientService.checkUser(userId, password)){
                        System.out.println("登录成功");
                        //进入二级菜单
                        while (loop){
                            System.out.println("**********用户" + userId + "列表**********");
                            System.out.println("**********1. 显示在线用户**********");
                            System.out.println("**********2. 群发消息**********");
                            System.out.println("**********3. 私聊**********");
                            System.out.println("**********4. 发送文件**********");
                            System.out.println("**********9. 退出**********");

                            System.out.print("请选择菜单：");
                            key = Utility.readString(1, "1");
                            switch (key){
                                case "1":
                                    System.out.println("显示在线用户");
                                    userClientService.getOnlineUser();
                                    break;
                                case "2":
                                    System.out.println("请输入想对大家说的话：");
                                    String msg = Utility.readString(150);
                                    MessageClientService.sendMessageToAll(msg, userId);
                                    break;
                                case "3":
                                    System.out.println("请输入私聊的用户名（在线）：");
                                    String privateUserId = Utility.readString(50);
                                    System.out.println("请输入私聊的消息：");
                                    String message = Utility.readString(150);
                                    MessageClientService.sendMessageToOne(message, userId, privateUserId);
                                    break;
                                case "4":
                                    System.out.println("发送文件给谁：");
                                    String receiverId = Utility.readString(50);
                                    System.out.println("发送源文件位置:");
                                    String filePath = Utility.readString(150);
                                    System.out.println("发送目标文件位置:");
                                    String targetFilePath = Utility.readString(150);
                                    FileClientService.sendFileToOne(filePath, targetFilePath, userId, receiverId);
                                    System.out.println("发送文件成功");
                                    break;
                                case "9":
                                    loop = false;
                                    userClientService.exit();
                                    System.out.println("退出系统");
                                    break;
                                default:
                                    System.out.println("选择错误，请重新输入：");
                            }
                        }
                    }else{
                        System.out.println(userId +  "登录失败");
                    }

                    break;
                case "9":
                    loop = false;
                    //退出线程

                    System.out.println("退出系统");
                    break;
                default:
                    System.out.println("选择错误，请重新输入：");
            }
        }
    }
}
